package com.yanqu.road.dao.impl.activity.bulletscreen;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.bulletscreen.BulletScreenUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BulletScreenUserDataDaoImpl extends TempDataObjectDao<BulletScreenUserData> {

    public Map<Integer, BulletScreenUserData> getBulletScreenUserData(long userId, List<Integer> activityIdList) {
        String getBulletScreenUserDataSql = "select * from t_u_activity_bullet_screen_user_data where userId=? and activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, BulletScreenUserData> resultMap = new ConcurrentHashMap<>();
        if(activityIdList.size() == 0){
            return resultMap;
        }
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getBulletScreenUserDataSql);
            int i = 1;
            call.setLong(i++,userId);
            rs = call.executeQuery();
            while (rs.next()) {
                BulletScreenUserData data = parse(rs);
                resultMap.put(data.getActivityId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }

    public static String insertBulletScreenUserDataSql = "insert into t_u_activity_bullet_screen_user_data(userId,activityId,resetTime,canBarrageTimes,buyBarrageTimes,readContentId,readContentIdList,lastReadTime) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(BulletScreenUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCanBarrageTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyBarrageTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReadContentId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getReadContentIdList(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastReadTime()));
        if (getDb().executeNoneQuery(insertBulletScreenUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateBulletScreenUserDataSql = "update t_u_activity_bullet_screen_user_data set resetTime=?,canBarrageTimes=?,buyBarrageTimes=?,readContentId=?,readContentIdList=?,lastReadTime=? where userId=? and activityId=?";

    @Override
    protected boolean updateData(BulletScreenUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCanBarrageTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyBarrageTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReadContentId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getReadContentIdList(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastReadTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateBulletScreenUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected BulletScreenUserData parseData(ResultSet rs) throws SQLException {
        BulletScreenUserData data = new BulletScreenUserData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setResetTime(rs.getLong("resetTime"));
        data.setCanBarrageTimes(rs.getInt("canBarrageTimes"));
        data.setBuyBarrageTimes(rs.getInt("buyBarrageTimes"));
        data.setReadContentId(rs.getLong("readContentId"));
        String idList = rs.getString("readContentIdList");
        if(!StringUtils.isNullOrEmpty(idList)){
            data.setReadContentIdList(StringUtils.stringToLongList(idList, ","));
        }
        data.setLastReadTime(rs.getLong("lastReadTime"));
        return data;
    }
}
